package com.duoketech.controller.weixin;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.service.web.order.OrderService;
import com.duoketech.service.weixin.activity.WeiXinActivityService;
import com.duoketech.service.weixin.room.WeiXinRoomService;
import com.duoketech.utils.pay.weixin.JSSDKInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Const;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class WxActivityController extends BaseController {

    public void index(){
        render("index.html");
    }

    public void ajaxActivity(){
        int pageNum = getParaToInt("pageSize",1);
        Page<Activity> activityList = WeiXinActivityService.ME.queryActivity(pageNum,10);
        Kv kv = Kv.by("activityList",activityList);
        renderObject(kv);
    }

    public void activityDetail(){
        int id = getParaToInt("id",0);
        setAttr("id",id);
        setAttr("activity",WeiXinActivityService.ME.queryActivityById(id));
        render("activity-detail.html");
    }

    public void queryActivityById(){
        int id = getParaToInt("id",0);
        Activity activity = WeiXinActivityService.ME.queryActivityById(id);
        renderObject(activity);
    }

    public void ajaxDoActivityOrder(){
        try {
            boolean result = validateToken(Constant.TOKEN_KEY);
            if(!result){
                renderError("请勿重复提交当前活动信息");
                return;
            }
            ActivityOrder activityOrder = getModel(ActivityOrder.class);
            activityOrder.set(ActivityOrder.MEMBER_ID,getMemberId());
            int id = OrderService.ME.doAddActivityOrder(activityOrder);
            renderObject(id);
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }


    public void activityOrder(){
        int id = getParaToInt("id",0);
        Activity activity = Activity.dao.findById(id);
        createToken(Constant.TOKEN_KEY);
        setAttr("activity",activity);
        render("activity-order.html");
    }


    public void cancelActivityOrder(){
        int id = getParaToInt("id",0);
        try {
            WeiXinActivityService.ME.cancelActivityOrder(id,getMemberId());
            renderSuccess("取消成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }


    @Before(JSSDKInterceptor.class)
    public void activityOrderPreview(){
        int id = getParaToInt("id",0);
        try {
            ActivityOrder order= WeiXinActivityService.ME.activityOrderPreview(id,getMemberId());
            setAttr("activityOrder",order);
            render("activity-order-preview.html");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }
}
